package top.wardyan.wdev.dbdevassist.action.base;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

import top.wardyan.action.base.AbstractShell;
import top.wardyan.base.model.BaseModel;
import top.wardyan.control.WButton;
import top.wardyan.control.WComposite;
import top.wardyan.control.WLabel;
import top.wardyan.control.WTable;
import top.wardyan.control.WTableItem;
import top.wardyan.control.WText;

/**
 * 使用sqlite记录表格列宽标题等信息
 * @author ward
 */
public abstract class BaseMasterDetailShell_BAK<T extends BaseMasterDetailShell_BAK<T>> extends BaseShell<T>{
	protected WComposite mainCom;
	protected WTable mainTable;
	protected WLabel mainInfo;
	protected WText mainSearchText;
	protected WButton btnMainAdd;
	
	public BaseMasterDetailShell_BAK() {
		super();
		this.createContent();
	}
	
	private void createContent() {
		
		mainCom = new WComposite(this, SWT.BORDER);
		mainCom.setLayout(new FormLayout());
		FormData fd_mainCom = new FormData();
		fd_mainCom.bottom = new FormAttachment(100, -1);
		fd_mainCom.left = new FormAttachment(this, 0);
		fd_mainCom.right = new FormAttachment(100, -1);
		fd_mainCom.top = new FormAttachment(topCom, 6);
		mainCom.setLayoutData(fd_mainCom);
		
		mainSearchText = new WText(mainCom, SWT.BORDER, "", "搜索关键字");
		FormData fd_mainSearchText = new FormData();
		fd_mainSearchText.bottom = new FormAttachment(mainSearchText, 30);
		fd_mainSearchText.top = new FormAttachment(mainCom, 2);
		fd_mainSearchText.left = new FormAttachment(mainCom, 2);
		fd_mainSearchText.right = new FormAttachment(100, -80);
		mainSearchText.setLayoutData(fd_mainSearchText);
		mainSearchText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				//self.listMain(mainTable, true);
				mainTable.show("mainTable", mainSearchText.getText());
			}
		});
		mainSearchText.setFocus();
		
		btnMainAdd = new WButton(mainCom, SWT.PUSH, "新增", new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				AbstractShell<?> win = mainEditShell(0);
				if(win != null) {
					new WDialog(self, win).open();
				}
				mainTable.show("mainTable", mainSearchText.getText(), self.getList());
			}
		});
		FormData fd_btnMainAdd = new FormData();
		fd_btnMainAdd.bottom = new FormAttachment(btnMainAdd, 30);
		fd_btnMainAdd.right = new FormAttachment(btnMainAdd, 70);
		fd_btnMainAdd.top = new FormAttachment(mainCom, 2);
		fd_btnMainAdd.left = new FormAttachment(mainSearchText, 6);
		btnMainAdd.setLayoutData(fd_btnMainAdd);
		
		mainTable = new WTable(mainCom, SWT.NONE, "mainTable");
		FormData fd_mainTable = new FormData();
		fd_mainTable.bottom = new FormAttachment(100, 0);
		fd_mainTable.right = new FormAttachment(100, 0);
		fd_mainTable.top = new FormAttachment(mainSearchText, 6);
		fd_mainTable.left = new FormAttachment(0);
		mainTable.setLayoutData(fd_mainTable);
		mainTable.setDatas(this.getList());
		//Menu menu = new Menu(mainTable);
		mainTable.show("mainTable", "");
		//mainTable.setm
		mainTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				int index = mainTable.getSelectionIndex();
				if(index >= 0) {
					TableItem item = mainTable.getItem(index);
					if(item != null) {
						TableColumn[] cols = mainTable.getColumns();
//						int cindex = 0;
//						if(cols != null) {
//							for(TableColumn c : cols) {
//								WTableColumn wc = (WTableColumn)c;
//								if(wc.getXField().equals("sID"))
//									break;
//								else
//									cindex++;
//							}
//						}
						//String sid = item.getText(cindex);
						
						//new WDialog(self, new UserEditShell(Integer.valueOf(sid))).open();
						// refresh
						AbstractShell<?> win = mainEditShell(((WTableItem)item).getModel().getInt("sID"));
						if(win != null) {
							new WDialog(self, win).open();
						}
						// self.listMain(mainTable, true);
						mainTable.show("mainTable", mainSearchText.getText(), self.getList());
					}
				}
			}
		});
		/**
		mainTable.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				//self.listCol();
			}
		});
		**/		
		//this.initMainColumn(mainTable);
		//this.listMain(mainTable, true);
		
		this.generatePopupMenu();
		//System.out.println(this.getClass().getPackageName());
		//System.out.println(this.getClass().getName());
	}
	
	//protected abstract void initMainColumn(WTable table);
	protected abstract List<? extends BaseModel<?>> getList();
	//protected abstract void listMain(WTable table, boolean force);
	
	// protected abstract void showMainAdd();
	protected abstract AbstractShell<? extends AbstractShell<?>> mainAddShell();
	protected abstract AbstractShell<? extends AbstractShell<?>> mainEditShell(int sID);
	
	protected void generatePopupMenu() {
	}

	@Override
	protected boolean isMainWindow() {
		return true;
	}
	
	
}
